package com.example.lym.syappandroid;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.baidu.android.pushservice.PushConstants;
import com.baidu.android.pushservice.PushManager;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Comparator;
import java.util.List;

import baidupush.Utils;
import model.Alarm;
import model.AlarmAdapter;
import model.User;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import regular.IRegulation;

public class AlarmlistActivity extends AppCompatActivity {

    int t_id;
    private List<Alarm> alarmList = new ArrayList<>();
    ListView lv_alarmlist;
    ImageView iv_alarmlist_last;
    TextView tv_alarmlist_name;

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityCollector.removeActivity(AlarmlistActivity.this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alarmlist);
        ActivityCollector.addActivity(AlarmlistActivity.this);
        Init();

    }
    /**
     * 初始化控件和事件
     */
    private void Init() {
        //初始化任务
        Intent intent = this.getIntent();
        tv_alarmlist_name = (TextView)findViewById(R.id.tv_alarmlist_name);
        tv_alarmlist_name.setText(intent.getStringExtra("t_name"));
        this.t_id = intent.getIntExtra("t_id",-1);
        
        //初始化上一页
        iv_alarmlist_last = (ImageView)findViewById(R.id.iv_alarmlist_last);
        iv_alarmlist_last.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityCollector.removeActivity(AlarmlistActivity.this);
                finish();
            }
        });

        //初始化提醒列表
        InitAlarmList();
    }

    /**
     * 初始化数据列表
     */
    private void InitAlarmList() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    RequestBody requestBody = new FormBody.Builder().add("u_id", User.getU_id()+"").add("t_id",t_id+"").build();
                    Request request = new Request.Builder().url(IRegulation.URL+"alarm/getlistbytaskid").post(requestBody).build();
                    Response response = client.newCall(request).execute();
                    String str = response.body().string();
                    JSONObject jsonObject = JSONObject.fromObject(str);
                    //验证jsonObject的code
                    if(IRegulation.FAILED.equals(jsonObject.getString("resultCode"))){//失败
                        Log.e("LYM","获取用户提醒列表失败");
                    }else if(IRegulation.LACKACCESS.equals(jsonObject.getString("resultCode"))){
                        Log.e("LYM","用户无权限");
                    }else if(IRegulation.SUCCESS.equals(jsonObject.getString("resultCode"))){
                        //获取数据
                        JSONArray jsonArray = jsonObject.getJSONArray("data");
                        for(int i=0;i<jsonArray.size();i++){
                            JSONObject temp = jsonArray.getJSONObject(i);
                            Alarm alarm = new Alarm(temp.getInt("a_isread")==1,temp.getInt("a_id"),temp.getString("a_name"), temp.getString("a_time")
                                    ,temp.getInt("u_id"),temp.getInt("t_id"),temp.getString("a_detail"),temp.getInt("a_ispush")==1);
                            if(alarm.isA_ispush())
                                alarmList.add(alarm);
                        }
                    }
                    Log.i("LYMHHH",alarmList.size()+"");
                    //初始化列表
                    InitAlarms();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (ParseException e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

    /**
     * 初始化提醒列表
     */
    private void InitAlarms() {
        runOnUiThread(new Runnable() {
            @SuppressWarnings("Since15")
            @Override
            public void run() {
                lv_alarmlist = (ListView) findViewById(R.id.lv_alarmlist);
                alarmList.sort(new Comparator<Alarm>() {
                    @Override
                    public int compare(Alarm o1, Alarm o2) {
                        return o2.getA_time().compareTo(o1.getA_time());
                    }
                });
                AlarmAdapter adapter = new AlarmAdapter(AlarmlistActivity.this,R.layout.alarm_item,alarmList);
                lv_alarmlist.setAdapter(adapter);
                lv_alarmlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Alarm alarm = alarmList.get(position);
                        Intent intent = new Intent(AlarmlistActivity.this,ShowalarmActivity.class);
                        intent.putExtra("a_id",alarm.getA_id());
                        startActivity(intent);
                    }
                });
            }
        });
    }
}
